#项目名称：使用【请求头】爬取豆瓣电影网Top100的电影

import requests
from bs4 import BeautifulSoup
import csv
# 设置要请求的网页链接
url = 'https://movie.douban.com/top250?start=0&filter='

# 设置请求头
heade = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
# 创建列表存储电影信息
movie_lists = []
for page_number in range(4):
    url = "https://movie.douban.com/top250?start={}&filter=".format(page_number*25)
    # 请求网页
    res = requests.get(url, headers=heade)
    # 解析网页
    bs = BeautifulSoup(res.text,"html.parser")
    # 提取每页电影信息
    movie_info = bs.find("ol",class_ = "grid_view").find_all("li")
    for movie in movie_info:
        # 提取电影编号
        movie_code = movie.find("em").text
        # 提取电影名
        movie_name = movie.find("span").text
        # 提取电影评分
        movie_star = movie.find("span",class_ = "rating_num").text
        # 提取电影推荐语
        movie_value = movie.find("span",class_ = "inq").text
        #提取电影链接
        movie_web = movie.find("a")["href"]
        # 创建字典存储电影信息
        dict = {
            "序号":movie_code
            ,"电影名":movie_name
            ,"评分":movie_star
            ,"推荐语":movie_value
            ,"链接":movie_web
        }
        # 打印每一部电影信息
        print(dict)
        # 将电影信息添加到列表中
        movie_lists.append(dict)
with open("movies.csv","w",encoding="utf-8",newline="") as f:
    writer = csv.DictWriter(f,["序号","电影名","评分","推荐语","链接"])
    # 写入表头
    writer.writeheader()
    writer.writerows(movie_lists)